using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;

namespace Universe.JRun
{
    [Serializable]
    public class JDescription
    {
        public enum JavaKind
        {
            JRE,
            JDK
        }

        private JavaKind _kind;
        private string _path;
        private string _version;
        private Version _productVersion;
        private bool _isCurrent;


        public JDescription()
        {
        }


        public JDescription(JavaKind kind, string path, string version, Version productVersion)
        {
            _kind = kind;
            _path = path;
            _version = version;
            _productVersion = productVersion;
        }

        public JavaKind Kind
        {
            get { return _kind; }
            set { _kind = value; }
        }

        public bool IsCurrent
        {
            get { return _isCurrent; }
            set { _isCurrent = value; }
        }

        public string Version
        {
            get { return _version; }
            set { _version = value; }
        }

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }


        public Version ProductVersion
        {
            get { return _productVersion; }
            set { _productVersion = value; }
        }

        public bool Exists
        {
            get
            {
                try
                {
                    return File.Exists(System.IO.Path.Combine(this.Path, @"bin\java.exe"));
                }
                catch
                {
                    return false;
                }
            }
        }


        public override string ToString()
        {
            return "{Java " + (_productVersion != null ? _productVersion.ToString() : Version) + ": '" + Path + "'}";
        }
    }

    [Serializable]
    public class JDescriptions: Collection<JDescription>
    {
        public JDescriptions()
        {
        }


        public JDescriptions(IEnumerable<JDescription> list) : base(new List<JDescription>(list))
        {
        }

        public JDescription Current
        {
            get
            {
                if (Count == 0)
                    throw new InvalidOperationException("Java not found");

                foreach (JDescription description in this)
                {
                    if (description.IsCurrent)
                        return description;
                }

                return Count == 0 ? null : this[0];
            }
        }

        public static JDescriptions JRE
        {
            get { return JImpl.Read(JImpl.JRE, JDescription.JavaKind.JRE); }
        }

        public static JDescriptions JDK
        {
            get { return JImpl.Read(JImpl.JDK, JDescription.JavaKind.JDK); }
        }
    }
}
